package com.android.mios.adapter;

import java.util.ArrayList;
import java.util.List;
import com.android.mios.R;
import com.android.mios.model.Engine;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class VeraListViewAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<Engine> mList;
	private int resource;
	//private Context context;
	private ArrayList<String> arrayOfSerail;

	public VeraListViewAdapter(final Context context, List<Engine> data,
			int resource, ArrayList<String> arrayOfSerail) {
		mInflater = LayoutInflater.from(context);
		mList = data;
		this.resource = resource;
		//this.context = context;
		this.arrayOfSerail = arrayOfSerail;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null)
			convertView = mInflater.inflate(resource, null);

		Engine engine = mList.get(position);
		
		String ip =engine.getIpAddress();
		
		TextView txt_vera_name = (TextView) convertView
				.findViewById(R.id.txt_vera_name);
		
		if(ip==null || ip.equals(""))
		     txt_vera_name.setText(engine.toString() + "("
				 + engine.getSerialNumber() + ")");
		else
			 txt_vera_name.setText(engine.getIpAddress() + "("
					 + engine.getSerialNumber() + ")");

		CheckBox checkbox = (CheckBox) convertView
				.findViewById(R.id.checkbox_vera);

		checkbox.setVisibility(View.VISIBLE);

		int iCurrentNumber = engine.getSerialNumber();

		if (arrayOfSerail.contains(iCurrentNumber+""))
			checkbox.setChecked(true);
		else
			checkbox.setChecked(false);

		return convertView;

	}

}
